﻿using DevExpress.XtraReports.UI;
using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Depo._7C
{
    public partial class xrSubModel : DevExpress.XtraReports.UI.XtraReport
    {
        public xrSubModel()
        {
            InitializeComponent();
        }

        DataSet ds;
        DataTable dtModel;
        public void fillTable(DataTable dt)
        {
            ds = new DataSet();
            dtModel = new DataTable("dtModel");
            ds.Tables.Add(dtModel);
            dtModel.Merge(dt);

            this.DataMember = dtModel.TableName;
            this.DataSource = ds;

            tcModel.BeforePrint += new System.Drawing.Printing.PrintEventHandler(tcModel_BeforePrint);
        }

        void tcModel_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            this.DataMember = dtModel.TableName;
            this.DataSource = ds;

            tcModel.DataBindings.Add("Text", null, "dtModel.Model");
            tcDosyaNo.DataBindings.Add("Text", null, "dtModel.DosyaNo");
            tcBM.DataBindings.Add("Text", null, "dtModel.BM", "{0:0.00}");
            tcAdet.DataBindings.Add("Text", null, "dtModel.GAdet");
            tcMetraj.DataBindings.Add("Text", null, "dtModel.Toplam", "{0:0.0}");
        }

        private void ReSize(object sender)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 9f;
            Font defaultFont = new System.Drawing.Font("Trebuchet MS", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.WidthF / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();
        }
    }
}
